package com.youyou.fusheng2.View.Fragment;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.youyou.fusheng2.R;
import com.youyou.fusheng2.Utils.ThisUrl;
import com.youyou.fusheng2.View.Activity.mine_public;
import com.youyou.fusheng2.View.Adapter.mineAdapter;

import java.util.ArrayList;
import java.util.List;

import static android.content.Context.CLIPBOARD_SERVICE;

public class MineFragment extends Fragment {

    private             RecyclerView listview;
    private             List<String> listtou            = new ArrayList();
    private             List<String> listitem           = new ArrayList<>();
    private             TextView     mainqq;
    private             TextView     mainWchart;
    public static final String       APP_PACKAGE_NAMEQQ = "com.tencent.mobileqq";
    public static final String       APP_PACKAGE_NAMEWX = "com.tencent.mm";


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.mine_fragment, null);
        initView(inflate);
        date();
        return inflate;
    }

    private void date() {
        listtou.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1584273340832&di=4b3497533ea6a25f7510d0ee70cefe00&imgtype=0&src=http%3A%2F%2Fwww.ghost64.com%2Fqqtupian%2FzixunImg%2Flocal%2F2017%2F03%2F27%2F14906062244087.jpg");
        listtou.add("格物");
        listitem.add("分享应用");
        listitem.add("隐私政策");
        listitem.add("版本信息");
        listitem.add("设置端口IP");
        listitem.add("客服QQ:2567294605");
        listitem.add("All you want is here copyright Anhui chuzhou.com welcome to recommend");
    }

    private void initView(View inflate) {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/9/1  17:13
         *  @简介:
         */
        listview = inflate.findViewById(R.id.mine_rec);
        inflate.findViewById(R.id.home_sousuo).setVisibility(View.GONE);
        mineAdapter recliViewAdapter = new mineAdapter(getContext(), listtou, listitem);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        listview.setAdapter(recliViewAdapter);
        listview.setLayoutManager(layoutManager);
        recliViewAdapter.notifyDataSetChanged();

        recliViewAdapter.setOnItemClickListener(new mineAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int postion) {
                if (postion == 2) {
                    Intent intent = new Intent(getContext(), mine_public.class);
                    intent.putExtra("id", postion);
                    startActivity(intent);
                } else if (postion == 1) {
                    setShowPupu(postion,1);
                } else if (postion == 3) {
                    Intent intent = new Intent(getContext(), mine_public.class);
                    intent.putExtra("id", postion);
                    startActivity(intent);
                } else if (postion == 4) {
                    setShowPupu2();
                } else  if (postion==5){
                    setShowPupu(postion,2);
                }
            }
        });
    }

    private void setShowPupu2() {
        View contentview = LayoutInflater.from(getContext()).inflate(R.layout.mineduankou_activity, null, false);
        PopupWindow popupWindow = new PopupWindow(contentview, (int) ((getResources().getDisplayMetrics().widthPixels)-250 ),
                300);
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.showAtLocation(listview.getChildAt(0), Gravity.CENTER, 0, 0);

        EditText input = contentview.findViewById(R.id.mine_input);
        Button NO = contentview.findViewById(R.id.NO);
        Button OK = contentview.findViewById(R.id.OK);

        NO.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindow.dismiss();
            }
        });

        OK.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (input.getText().toString().isEmpty()){
                    Toast.makeText(getContext(), "IP为空", Toast.LENGTH_SHORT).show();
                }else {
                    new ThisUrl().setUrl(input.getText().toString().trim());
                    popupWindow.dismiss();
                }
            }
        });


    }

    private void setShowPupu(int i, int postion) {
        View contentview = LayoutInflater.from(getContext()).inflate(R.layout.dialog_fenxiang, null, false);
        PopupWindow popupWindow = new PopupWindow(contentview, (int) ((getResources().getDisplayMetrics().widthPixels) - 350),
                WindowManager.LayoutParams.WRAP_CONTENT);
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.showAtLocation(listview.getChildAt(postion), Gravity.CENTER, 0, 0);

        mainqq = contentview.findViewById(R.id.main_qq);
        mainWchart = contentview.findViewById(R.id.main_wchart);
        mainWchart = contentview.findViewById(R.id.main_wchart);
        mainqq.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isAppInstalled(APP_PACKAGE_NAMEQQ)) {
                    //如果有根据包名跳转
                    ClipboardManager clipboardManager = (ClipboardManager) getContext().getSystemService(CLIPBOARD_SERVICE);
                    clipboardManager.setPrimaryClip(ClipData.newPlainText("wx", "支持一下吧：浮生影视，百度搜索下载哦。支持一下开发者吧。"));
                    startActivity(getActivity().getPackageManager().getLaunchIntentForPackage(APP_PACKAGE_NAMEQQ));
                    popupWindow.dismiss();
                } else {
                    Toast.makeText(getContext(), "暂时未安装QQ", Toast.LENGTH_SHORT).show();
                }
            }
        });

        mainWchart.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onClick(View view) {
                if (isAppInstalled(APP_PACKAGE_NAMEWX)) {
                    //如果有根据包名跳转
                    ClipboardManager clipboardManager = (ClipboardManager) getContext().getSystemService(CLIPBOARD_SERVICE);
                    clipboardManager.setPrimaryClip(ClipData.newPlainText("qq", "支持一下吧：浮生影视，百度搜索下载哦。支持一下开发者吧。"));
                    startActivity(getActivity().getPackageManager().getLaunchIntentForPackage(APP_PACKAGE_NAMEWX));
                    popupWindow.dismiss();
                } else {
                    Toast.makeText(getContext(), "暂时未安装微信", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

    private boolean isAppInstalled(String packageName) {
        try {
            getActivity().getPackageManager().getPackageInfo(packageName, 0);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}